package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.PublicUtil;
import com.bangdream.michelia.view.activity.currency.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/18 15:04
 * @email 909732184@qq.com
 * 直播课程网格列表页的适配器
 */
public class CurriculumVideoGridItemAdapter extends RecyclerView.Adapter {
    private List<LiveBean> data = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private OnViewItemClickListener onViewItemClickListener;
    public OnViewItemClickListener getOnViewItemClickListener() {
        return onViewItemClickListener;
    }

    public List<LiveBean> getData() {
        return data;
    }

    public void setData(List<LiveBean> data) {
        this.data = data;
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public interface OnViewItemClickListener{
        void onCurriculumItemClick(int position);
    }

    public CurriculumVideoGridItemAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.curriculum_grid_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;
            //如果是第一条数据 宽度占两列的宽度
            int itemWidth = MainActivity.screenWidth - PublicUtil.dip2px(context,20);
            if(position!=0){
                itemWidth = MainActivity.screenWidth/2 - PublicUtil.dip2px(context,20);
            }
            //(212/350) = 0.6
            viewHolder.params.height = (int)(itemWidth*0.6);
            viewHolder.relatLayout.setLayoutParams(viewHolder.params);
            viewHolder.ivIcon.setLayoutParams(viewHolder.params);


            LiveBean bean = data.get(position);
            LiveBean.LivePlayTeacherListBean teacherBean =
                    (bean.getLivePlayTeacherList()==null||bean.getLivePlayTeacherList().size()<1)?null:bean.getLivePlayTeacherList().get(0);


            viewHolder.tvTeacherName.setText("讲师："+(teacherBean==null?"":teacherBean.getTeacherName()+""));
            viewHolder.tvTitle.setText(bean.getName()+"");
            //viewHolder.tvPrice.setText("¥"+nmPrice);
            viewHolder.tvWatchNumber.setText(""+bean.getLivePersonCnt());
            ImageLoadUtils.loadRoundCornerImage(context,data.get(position).getCover(),viewHolder.ivIcon);
        }
    }

    @Override
    public int getItemCount() {
        int size=0;
        if(data!=null){
            size=data.size()>3?3:data.size();
        }
        return size;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private ImageView ivIcon;
        private RelativeLayout relatLayout;
        private TextView tvPrice;
        private TextView tvTitle;
        private TextView tvTeacherName;
        private TextView tvWatchNumber;
        private ViewGroup.LayoutParams params;
        public ViewHolder(View itemView) {
            super(itemView);
            relatLayout = itemView.findViewById(R.id.relatLayout);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            tvPrice = itemView.findViewById(R.id.tvPrice);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvTeacherName = itemView.findViewById(R.id.tvTeacherName);
            tvWatchNumber = itemView.findViewById(R.id.tvWatchNumber);
            params = ivIcon.getLayoutParams();

            tvPrice.setVisibility(View.GONE);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onViewItemClickListener!=null){
                        onViewItemClickListener.onCurriculumItemClick(position);
                    }
                }
            });
        }
    }
}
